using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using UniversalAdmin.Application.Commands;
using UniversalAdmin.Application.Dto;
using UniversalAdmin.Application.Services;
using UniversalAdmin.Shared.Enums;
using UniversalAdmin.Api.Filters;
using UniversalAdmin.Domain.Entities.App;
using UniversalAdmin.Infrastructure.Data.Contexts;

[ApiController]
[Route("api/system/logs")]
[Authorize]
public class OperationLogsController : ControllerBase
{
    private readonly IAppOperationLogService _operationLogService;
    private readonly AdminDbContext _dbContext;
    
    public OperationLogsController(IAppOperationLogService operationLogService, AdminDbContext dbContext) 
    { 
        _operationLogService = operationLogService;
        _dbContext = dbContext;
    }

    [HttpGet]
    [RequirePermission("log.view")] // 需要查看日志权限
    public async Task<ApiResult> Get(
        [FromQuery] int? page = null, 
        [FromQuery] int? size = null, 
        [FromQuery] string? search = null,
        [FromQuery] string? level = null,
        [FromQuery] string? module = null,
        [FromQuery] string? startTime = null,
        [FromQuery] string? endTime = null)
    {
        if (page.HasValue && size.HasValue)
        {
            var pagedQuery = new PagedQuery { 
                Page = page.Value, 
                Size = size.Value, 
                Search = search,
                Level = level,
                Module = module,
                StartTime = startTime,
                EndTime = endTime
            };
            var result = await _operationLogService.GetPagedAsync(pagedQuery);
            return new ApiResult { Code = result.Code, Message = result.Message, Data = result.Data, Timestamp = result.Timestamp };
        }
        else
        {
            var result = await _operationLogService.GetAllAsync(search);
            return new ApiResult { Code = result.Code, Message = result.Message, Data = result.Data, Timestamp = result.Timestamp };
        }
    }

    [HttpGet("{id}")]
    [RequirePermission("log.view")] // 需要查看日志权限
    public async Task<ApiResult<OperationLogDto>> GetById(Guid id)
        => await _operationLogService.GetByIdAsync(id);

    [HttpPost("test")]
    [AllowAnonymous]
    public async Task<ApiResult> TestLog()
    {
        try
        {
            var testLog = new AppOperationLog
            {
                Id = Guid.NewGuid(),
                UserId = Guid.NewGuid(),
                Action = "测试操作",
                Operation = "POST /api/system/logs/test",
                Resource = "System",
                ResourceId = null,
                IpAddress = "127.0.0.1",
                UserAgent = "Test-Agent",
                LogLevel = "info",
                Module = "SYSTEM",
                Message = "测试日志记录",
                Details = "这是一个测试日志",
                Exception = null,
                RequestBody = "{}",
                ResponseBody = null,
                StatusCode = 200,
                ExecutionTime = 100,
                CreatedAt = DateTime.UtcNow
            };

            _dbContext.AppOperationLogs.Add(testLog);
            await _dbContext.SaveChangesAsync();

            return new ApiResult { Code = 200, Message = "测试日志已创建", Data = testLog.Id };
        }
        catch (Exception ex)
        {
            return new ApiResult { Code = 500, Message = $"创建测试日志失败: {ex.Message}" };
        }
    }
} 